﻿/// <summary>
// DBLinq.MySqlProviders - .NET Providers for MySql using the framework DBLinq under MIT license, author Jiri George Moudry
// http://linq.to/db
// and the CodeFx.MySqlProviders under GNU licence, author Laurent Morisseau
// http://www.codeplex.com/CodeFxMySqlProvider
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>
/// 
using DBLinq.MySqlProviders;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Specialized;
using MySql.Data.MySqlClient;
using System;

namespace DBLinq.MySqlProviders.TestProject
{
    
    
    /// <summary>
    ///This is a test class for AspNetUsersDTOTest and is intended
    ///to contain all AspNetUsersDTOTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AspNetUsersDTOTest
    {

        private MySqlConnection conn;
        private uint appid = 0;
        private uint userid = 0;

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        #region Additional test attributes
        [TestInitialize()]
        public void MyTestInitialize()
        {
            ConnectionString.Current.CurrentString = TestHelper.connectionstring;
            conn = TestHelper.Open();
            TestHelper.DeleteMemberTest(conn);
            TestHelper.DeleteApplicationTest(conn);
            appid = TestHelper.InsertApplicationTest(conn);
            userid = TestHelper.InsertMemberTest(conn, appid);

        }

        [TestCleanup()]
        public void MyTestCleanup()
        {

            TestHelper.DeleteMemberTest(conn);
            TestHelper.DeleteApplicationTest(conn);
            TestHelper.Close(conn);
        }

        #endregion

        /// <summary>
        ///A test for UsersInRoles_GetUsersInRoles
        ///</summary>
        [TestMethod()]
        public void UsersInRoles_GetUsersInRolesTest()
        {
            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);
            TestHelper.InsertUserInRole(conn, userid, roleid);

            /// Tests
            try
            {
                StringCollection actual = AspNetUsersDTO.UsersInRoles_GetUsersInRoles(appid, roleid);
                Assert.IsNotNull(actual);
                Assert.AreEqual<string>(actual[0], TestHelper.UserName);
            }
            finally
            {
                /// Finally
                TestHelper.DeleteUserinRole(conn, userid);
                TestHelper.DeleteRoleTest(conn, appid);
            }
        }

        /// <summary>
        ///A test for UsersInRoles_FindUsersInRole
        ///</summary>
        [TestMethod()]
        public void UsersInRoles_FindUsersInRoleTest()
        {
            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);
            TestHelper.InsertUserInRole(conn, userid, roleid);

            /// Tests
            StringCollection actual = AspNetUsersDTO.UsersInRoles_FindUsersInRole(appid, roleid, TestHelper.UserName);
            Assert.IsNotNull(actual);
            Assert.AreEqual<string>(actual[0], TestHelper.UserName);

            /// Finally
            TestHelper.DeleteUserinRole(conn, userid);
            TestHelper.DeleteRoleTest(conn, appid );
        }

        /// <summary>
        ///A test for Update_LastActivityDate
        ///</summary>
        [TestMethod()]
        [DeploymentItem("DBLinq.MySqlProviders.dll")]
        public void Update_LastActivityDateTest()
        {
            /// Init
            userid = TestHelper.InsertMemberTest(conn, appid);
            AspNetUser member = AspNetUsersDTO.Select (userid);

            DateTime initial = member.LastActivityDate;

            /// Test
            AspNetUsersDTO_Accessor.Update_LastActivityDate(userid);
           
            try
            {
                MySqlCommand cmd = new MySqlCommand("SELECT LastActivityDate FROM AspNet_users WHERE userid=?userid", conn);
                cmd.Parameters.Add(new MySqlParameter("?userid", userid));
                object reader = cmd.ExecuteScalar();
                Assert.IsNotNull(reader);
                
                Assert.IsTrue (Convert.ToDateTime(reader).CompareTo(initial)<0);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                TestHelper.DeleteMemberTest(conn);
            }
        }

        /// <summary>
        ///A test for SelectByFk_ApplicationId_UserNameWithPersistance
        ///</summary>
        [TestMethod()]
        public void SelectByFk_ApplicationId_UserNameWithPersistanceTest()
        {
            /// Init
            userid = TestHelper.InsertMemberTest(conn, appid);
            try
            {
                AspNetUser actual = AspNetUsersDTO.SelectByFk_ApplicationId_UserNameWithPersistance(appid, "DummyName");
                Assert.IsNull(actual);
                
                actual = AspNetUsersDTO.SelectByFk_ApplicationId_UserNameWithPersistance(appid, TestHelper.UserName);
                Assert.IsNotNull(actual);
                Assert.AreEqual(actual.UserName, TestHelper.UserName);
            }
            finally
            {
                TestHelper.DeleteMemberTest(conn);
            }
        }

        /// <summary>
        ///A test for Select
        ///</summary>
        [TestMethod()]
        public void SelectTest()
        {
            /// Init
            userid = TestHelper.InsertMemberTest(conn, appid);
            AspNetUser actual = actual = AspNetUsersDTO.Select(userid);
            Assert.IsNotNull(actual);
            Assert.AreEqual<uint>(actual.UserId , userid);
            Assert.AreEqual<string>(actual.UserName, TestHelper.UserName);
            TestHelper.DeleteMemberTest(conn);

        }

        /// <summary>
        ///A test for Insert
        ///</summary>
        [TestMethod()]
        public void InsertTest()
        {
            AspNetUser user = new AspNetUser();
            user.ApplicationId = appid;
            user.UserName  = TestHelper.UserName;
            user.IsAnonymous = true;
            user.LastActivityDate = DateTime.UtcNow;
            AspNetUsersDTO.Insert(user);
    
            try
            {
                MySqlCommand cmd = new MySqlCommand("SELECT username FROM AspNet_Users WHERE applicationid=?appid order by userid desc limit 0,1", conn);
                cmd.Parameters.Add(new MySqlParameter("?appid", appid));
                object reader = cmd.ExecuteScalar();
                Assert.IsNotNull(reader);
                Assert.AreEqual(TestHelper.UserName, reader);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                TestHelper.DeleteMemberTest (conn);
            }
	
        }

        /// <summary>
        ///A test for GetUserId
        ///</summary>
        [TestMethod()]
        public void GetUserIdTest()
        {
            uint actual = AspNetUsersDTO.GetUserId(TestHelper.UserName, appid);
            Assert.IsNotNull(actual);
            Assert.AreEqual<uint>(actual, userid);
            //TestHelper.DeleteMemberTest(conn);
        }

        /// <summary>
        ///A test for GetLastId
        ///</summary>
        [TestMethod()]
        public void GetLastIdTest()
        {
            AspNetUser user = new AspNetUser();
            user.ApplicationId = appid;
            user.UserName = TestHelper.UserName;

            userid = TestHelper.InsertMemberTest(conn, appid);
            uint actual = AspNetUsersDTO.GetLastId(user);
            Assert.IsNotNull(actual);
            Assert.AreEqual<uint>(actual, userid);
            TestHelper.DeleteMemberTest(conn);
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod()]
        public void DeleteTest()
        {

            userid = TestHelper.InsertUserTest(conn, appid);

            AspNetUsersDTO.Delete(userid);

            try
            {
                MySqlCommand cmd = new MySqlCommand("SELECT userid FROM AspNet_Users WHERE userid=?userid", conn);
                cmd.Parameters.Add(new MySqlParameter("?userid", userid));
                object reader = cmd.ExecuteScalar();
                Assert.IsNull(reader);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                TestHelper.DeleteMemberTest(conn);
                TestHelper.DeleteUserTest(conn);
            }
        }
    }
}
